package com.yyh.fanshe;

import java.lang.reflect.Method;

public class Demo4 {
    public static void main(String[] args) throws Exception {
        //1.获取Class对象
        Class clas1 = Class.forName("com.yyh.fanshe.User");
        //2.分别获得getName()和setName()方法
        Method getName = clas1.getMethod("getName");
        Method setName = clas1.getMethod("setName", String.class);
        System.out.println("getName = " + getName);
        System.out.println("setName = " + setName);
        System.out.println("------------------------------------------------");
        //3.获取所有的public修饰的方法： clas1.getMethods() ： 把父类Object中继承过来的也获取了
        Method[] methods = clas1.getMethods();
        for (Method method : methods) {
            System.out.println("method = " + method);
        }
        System.out.println("------------------------------------------------");
        //4.分别获取私有的getAge()和setAge()对象
        Method getAge = clas1.getDeclaredMethod("getAge");
        Method setAge = clas1.getDeclaredMethod("setAge", int.class);
        System.out.println("getAge = " + getAge);
        System.out.println("setAge = " + setAge);
        System.out.println("------------------------------------------------");
        //5.获取所有的构造方法对象：无法获取父类Object中的构造方法，只能获取本类中的私有和公有构造方法对象
        Method[] declaredMethods = clas1.getDeclaredMethods();
        for (Method method : declaredMethods) {
            System.out.println("method = " + method);
        }

    }
}
